import { mapGetters, mapActions } from "vuex"
export const mixins = {
    data() {
        return {
            book: null,
            moveX: 0,
            isPressSwiper: false,
        }
    },
    mounted() {
        this.saveRead()
        document.addEventListener('pause', () => {
            this.stopped = true
        }, false)
        document.addEventListener('resume', function () {
            this.stopped = false
        }, false)
        this.book = $('#bb-bookblock')
        this.book.bookblock({
            speed: 800,
            shadowSides: 0.8,
            shadowFlip: 0.7,
            shadows: true,
            current: this.$route.query.index ? this.$route.query.index : 0,
            onEndFlip: (old, page) => {
                //修改当前绘本页
                this.updateContentIndex(page)
            }
        })
    },
    //组件销毁暂停音乐
    beforeDestroy() {
        this.paused = true
        this.pauseAudio()
    },
    computed: {
        assignmentIsShow() {
            const assignmentPage = this.assignment[this.questionIndex]
            return assignmentPage ? assignmentPage.filter(item => { return !item.isFinish }).length > 0 ? true : false : false
        },
        ...mapGetters("contentStatus", {
            contentItem: "getContentItem",
            contentIndex: "getContentIndex",
            questionIndex: "getQuestionIndex"
        }),
        ...mapGetters("mainStatus", {
            readDurationMs: "getReadDurationMs"
        }),
        ...mapGetters("picbookListStatus", {
            picbookIndex: "getPicbookIndex"
        }),
        ...mapGetters("assignmentStatus", {
            assignment: "getAssignment"
        }),
        ...mapGetters("personalStatus",{
            userVip: "getUserVip",
            userVipList: "getUserVipList"
        }),
        ...mapGetters("seedActivityStatus", {
            isSeedEntery: "getIsSeedEntery"
        }),
    },
    methods: {
        start: function (event) {
            this.moveX = event.changedTouches[0].clientX
        },
        move: function (event) {
            if(this.picbookIndex.firstSwiper === undefined && this.assignmentIsShow && this.picbookIndex.vipFreeType !== 1){
                this.$set(this.picbookIndex,'firstSwiper',true)
            }
            if (this.isPressSwiper || this.picbookIndex.firstSwiper) return
            if (event.changedTouches[0].clientX - this.moveX > 50) {
                this.bookMusic()
                if (this.contentIndex > 0) {
                    //暂停所有音乐
                    this.pauseAudio()
                }
                this.book.bookblock('prev')
                this.isPressSwiper = true
            }
            if (event.changedTouches[0].clientX - this.moveX < - 50) {
                this.bookMusic()
                this.book.bookblock('next')
                this.isPressSwiper = true
                if (this.contentIndex + 1 < this.contentItem.length) {
                    //暂停所有音乐
                    this.pauseAudio()
                } else {
                    //暂停所有音乐
                    this.pauseAudio()
                    setTimeout(() => {
                        if (this.isSeedEntery) {
                            this.setPopup({
                                content: "你已经阅读完该绘本了",
                                callback: () => {
                                    this.$emit('assignmentTouch')
                                }
                            })
                        } else {
                            if(!this.userVipList[2].isActive && this.picbookIndex.vipFreeType === 3){
                                this.setUserFreeEntery(true)
                                const url = window.location.href
                                if(url.indexOf('beforeRouter=recommendList') > 0){
                                    this.setUserRouter('/recommendList')
                                }
                                if(url.indexOf('beforeRouter=picbookList') > 0){
                                    this.setUserRouter('/picbookList')
                                }
                                this.loadMastery()
                                this.setLoading({
                                    callback: () => {
                                        this.$router.push({ path: "/personal/study"})
                                    }
                                })
                            } else {
                                this.$emit('popupShowTouch')
                            }
                        }

                    }, 500)
                }
            }

        },
        end: function () {
            this.moveX = 0
            this.isPressSwiper = false
        },
        saveReadDuration: function (option) {
            option.onSuccess(this.readDurationMs)
            option.onComplete()
        },
        //进入绘本 开始计时
        saveRead: async function () {
            const json = {
                deviceUuid: this.deviceUuid,
                picbookId: this.picbookIndex.id
            }
            await this.awaitWarp(this.$post(this.api.addPicbookTime, json))
            setTimeout(() => { this.saveRead() }, this.readDurationMs)
        },
        //暂停所有音频
        pauseAudio: function () {
            this.diologMusic({
                play: false
            })
            this.parseTextAudio({
                play: false
            })
            this.picbookAudioMusic({
                play: false
            })
            this.questionMusic(false)
            this.answerMusic(false)
        },
        //用户评测结果
        loadMastery: async function () {
            const json = {
                deviceUuid: this.deviceUuid,
                isInSeedActivity: this.isSeedEntery                  // 如果为 false，表示是普通模式
                // 如果为 true，表示是通过【活动 - 种子用户孵化】进入的
            }
            const [data, err] = await this.awaitWarp(this.$post(this.api.loadMastery, json))
            if (data) {
                this.setUserMastery(data.mastery)
                this.studyExplain(data.abilityAndExplainationMap)
            }
        },
        ...mapActions("mainStatus", ["setPopup","setLoading"]),
        ...mapActions("contentStatus", ["updateContentIndex"]),
        ...mapActions("personalStatus",["setUserRouter","setUserFreeEntery","setUserMastery", "studyExplain"])
    }
}
